<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.mysql.jdbc.Driver"%>
<%@page import="java.sql.*"%>
<%@page import="com.mysql.jdbc.ResultSetMetaData"%>

<%! 
  String strTo(String st) throws Exception{ 
     byte[] bytes=st.getBytes("ISO-8859-1"); 
     String string=new String(bytes,"utf-8");
     return string;
  } 
  ArrayList<String[]> getDBItemNums(Connection conn)throws Exception{
	 ArrayList<String[]> list=new ArrayList<String[]>();
	 String[] strings=new String[2];
	 Statement st=conn.createStatement();
	 ResultSet rs=st.executeQuery("select * from comment");
	 while(rs.next()){ 
       strings[0]=rs.getObject(2).toString();
       strings[1]=rs.getObject(3).toString();
       list.add(strings);
	 }
	 return list;
  }
  void DBOperate(Connection conn,String clause)throws Exception{ 
	 Statement st=conn.createStatement();
	 int rs=st.executeUpdate(clause);
 }
 %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";

    /** 链接数据库参数 **/
    String driverName = "com.mysql.jdbc.Driver"; //驱动名称
    String DBUser = "root"; //mysql用户名
    String DBPasswd = ""; //mysql密码
    String DBName = "sanhaolu"; //数据库名

    //数据库完整链接地址
    String connUrl = "jdbc:mysql://localhost/" + DBName + "?user="
            + DBUser + "&password=" + DBPasswd;

    //加载数据库驱动
    Class.forName(driverName).newInstance();

    //链接数据库并保存到 conn 变量中
    Connection conn = DriverManager.getConnection(connUrl);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Transfer.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body >
   <% 
    request.setCharacterEncoding("ISO-8859-1");
    String flag =strTo(request.getParameter("flag"));
    String name = strTo(request.getParameter("name"));
	String content =strTo((String)request.getParameter("content"));
	//向数据库加入数据
	if(name!=null&&content!=null&&flag.equals("add")){
	String sql="INSERT into comment(person,comment_content) values('"+name+"','"+content+"')";
	DBOperate(conn,sql);
	}
	//从数据库删除数据 
	if(name!=null&&content!=null&&flag.equals("minus")){ 
	String sql="delete from comment where person='"+name+"'";
	DBOperate(conn,sql);
	}
	response.sendRedirect("sasion.jsp");
   %>
  </body>
</html>
